package com.yunxiu.controller;
import com.yunxiu.entity.User;
import com.yunxiu.service.IUserServicer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * 用户类mvc
 * @author Deom
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    private IUserServicer iUserServicer;

    @RequestMapping("/Tests.do")
    public String Tests() {
        return "redirect:/LoginInfo/index.do";
    }

    @RequestMapping("/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
        String url="";
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String number=request.getParameter("number");
        String pwd=request.getParameter("pwd");
        PrintWriter out = response.getWriter();
        boolean flg=iUserServicer.logion(number,pwd);
        if (flg){
            //密码和账号正确，跳转到首页
            User byId = iUserServicer.findById(Integer.parseInt(number));
            //查询到该用户所有信息
            session.setAttribute("user",byId);
            return "index";
        }else {
            System.out.println("密码错误");
            out.print("<script>alert('账号或密码错误');</script>");
        }
        return "redirect:/login.jsp";
    }


    /**
     * 修改密码
     */
    @RequestMapping("updatePwd")
    public void updatePwd(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //获取账号
        User user = (User) session.getAttribute("user");
        //获取旧密码
        String oldPwd=request.getParameter("oldPwd");
        //获取新密码
        String pwd = request.getParameter("pwd");
        //获取到确认密码
        String Rpwd = request.getParameter("Rpwd");
        if (oldPwd.equals(user.getUPassword())) {
            if (Rpwd.equals(pwd)) {
                try{
                    int row = iUserServicer.update(String.valueOf(user.getUNumber()), Rpwd);
                    System.out.println(user.getUNumber());
                    System.out.println(row);
                    if (row > 0) {
                        out.print("<script>alert('密码修改成功！请重新登入');location.href='/login.jsp'</script>");
                        //return "redirect:/login.jsp";
                    } else {
                        out.print("<script>alert('由于某种原因修改失败')</script>");
                    }
                }catch (Exception e){
                   e.printStackTrace();
                }
            } else {
                out.print("<script>alert('两次密码不一致');location.href='/index/changepwd.do'</script>");
            }
        }else {
            out.print("<script>alert('请输入正确的原密码！！！');location.href='/index/changepwd.do'</script>");
        }
    }
}
